package com.abel.bigwater.dataserver.model.qdies

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.dataserver.model.PacketDataLenException
import io.netty.buffer.ByteBuf

class D10LoginData : D10BaseData(6 + 4) {

    companion object {
        const val MIN_LEN = 6 + 4
        const val PREFIX = "IESD10"
    }

    override var type = FuncType.Login

    override fun count(): Int {
        assert(address != null)
        return MIN_LEN
    }

    var address: BcdString? = null

    override fun fill(buf: ByteBuf) {
        if (address?.size != 4) throw PacketDataLenException("len of login is bad: ${address?.size}")

        buf.writeBytes(PREFIX.toByteArray())
        buf.writeBytes(address!!.hexArray)
    }

    override fun parse(buf: ByteBuf): Boolean {
        if (len != MIN_LEN) throw PacketDataLenException("len of Login is bad: ${len}")

        assert(PREFIX.equals(String(ByteArray(PREFIX.length).also { buf.readBytes(it) })))
        address = BcdString.create(buf, 4)
        return true
    }
}